package com.oracle.cmp.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.oracle.cmp.common.Constant;
import com.oracle.cmp.common.PageInfo;
import com.oracle.cmp.common.Result;
import com.oracle.cmp.entity.Code;
import com.oracle.cmp.entity.Order;
import com.oracle.cmp.entity.OrderDetail;
import com.oracle.cmp.entity.User;
import com.oracle.cmp.service.*;
import com.oracle.jdbc.util.Dao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

@WebServlet("/order.do")
public class OrderColler extends HttpServlet{

    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String op = request.getParameter("op");
        if("query".equals(op)){
            query(request,response);
        }else if("delete".equals(op)) {
            delete(request, response);
        }else if("edit".equals(op)) {
            edit(request, response);
        }else if("add".equals(op)) {
           add(request, response);
        }else if ("save".equals(op)) {
            save(request, response);
        }else if ("update".equals(op)) {
            update(request, response);
        }else if ("checkquery".equals(op)){
            checkquery(request,response);
        } else if ("checkeup".equals(op)) {
            checkeup(request, response);
        }else if ("checkno".equals(op)) {
            checkno(request, response);
        }else if ("sbmit".equals(op)) {
            sbmit(request, response);
        }
    }

    public void query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        OrderService orderService=new OrderServicelmpl();
        String pageNo = request.getParameter("pageNum");
        String ordercode=request.getParameter("ordercode");
        String orderdate=request.getParameter("orderdate");
        String orderflag=request.getParameter("orderflag");
        int pageNum = 1;
        if(pageNo!=null)
            pageNum = Integer.parseInt(pageNo);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("ordercode",ordercode);
        map.put("orderdate",orderdate);
        map.put("orderflag",orderflag);
        CodeService codeService=new CodeServiceImpl();
        List<Code> oderFlags =codeService.query("order");
        map.put("oderFlags",oderFlags);
        Result result =orderService.queryForPage(map,pageNum);
        int total = orderService.count(map);
        int pages = (total+ Constant.PAGESIZE-1)/Constant.PAGESIZE;
        PageInfo pageInfo = new PageInfo();
        pageInfo.setList((List)result.getData());
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(Constant.PAGESIZE);
        pageInfo.setTotal(total);
        pageInfo.setPages(pages);
        request.setAttribute("pageInfo",pageInfo);
        request.setAttribute("ordercode",ordercode);
        request.setAttribute("orderdate",orderdate);
        request.setAttribute("orderflag",orderflag);
       request.setAttribute("orderFlags",oderFlags);

        request.getRequestDispatcher("/page/ordersys/order/orderlist.jsp").forward(request,response);
    }
    private void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException {
        String orderid=request.getParameter("orderid");
        OredrDetailService oredrDetailService=new OredrDetailmpl();
       List<OrderDetail> list=oredrDetailService.queryOrdeId(Integer.parseInt(orderid));
       OrderService orderService=new OrderServicelmpl();
       Result result=orderService.selectById(Integer.parseInt(orderid));
       request.setAttribute("order",result.getData());
       request.setAttribute("orderdetail",list);
        request.getRequestDispatcher("/page/ordersys/order/orderedit.jsp").forward(request,response);
    }
    private void save(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException {
        String ordercode = request.getParameter("ordercode");
        String orderflag = request.getParameter("orderflag");
        String[] partsids = request.getParameterValues("partsid");
        String[] counts = request.getParameterValues("ordercount");
        OrderService orderService = new OrderServicelmpl();
        Order order = new Order();
        order.setOrdercode(ordercode);
        order.setOrderflag(orderflag);
        order.setOrderdate(new Date());
        List<OrderDetail> details = new ArrayList<OrderDetail>();
        for(int i = 0;i<partsids.length;i++){
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setPartsid(Integer.parseInt(partsids[i]));
            orderDetail.setOrderpartscount(Integer.parseInt(counts[i]));
            details.add(orderDetail);
        }
        //User user = (User)request.getSession().getAttribute("user");
        User user= (User)request.getSession().getAttribute("user");
        User loginuser=new LoginService().queryID(user.getLoginname());
        Result result = orderService.save(order,details,loginuser.getUserid());
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonStr = objectMapper.writeValueAsString(result);
        response.getWriter().print(jsonStr);
    }
    private void update(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException  {
        String orederid=request.getParameter("orderid");
        String ordercode = request.getParameter("ordercode");
        String orderflag = request.getParameter("orderflag");
        String[] partsids = request.getParameterValues("partsid");
        String[] counts = request.getParameterValues("ordercount");
        OrderService orderService = new OrderServicelmpl();
        Order order = new Order();
        order.setOrderid(Integer.parseInt(orederid));
        order.setOrdercode(ordercode);
        order.setOrderflag(orderflag);
        order.setOrderdate(new Date());
        List<OrderDetail> details = new ArrayList<OrderDetail>();
        for(int i = 0;i<partsids.length;i++){
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderid(Integer.parseInt(orederid));
            orderDetail.setPartsid(Integer.parseInt(partsids[i]));
            orderDetail.setOrderpartscount(Integer.parseInt(counts[i]));
            details.add(orderDetail);
        }
        User user= (User)request.getSession().getAttribute("user");
        User loginuser=new LoginService().queryID(user.getLoginname());
        String usedid=String.valueOf(loginuser.getUserid());
        Result result = orderService.update(order,details,usedid);
        result.setData(orederid);
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonStr = objectMapper.writeValueAsString(result);
        response.getWriter().print(jsonStr);
    }
    private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/page/ordersys/order/orderadd.jsp").forward(request,response);
    }
    private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String orederid=request.getParameter("orderid");
        User user= (User)request.getSession().getAttribute("user");
        User loginuser=new LoginService().queryID(user.getLoginname());
        String userid=Integer.toString(loginuser.getUserid());
        OrderService orderService = new OrderServicelmpl();
        orderService.delete(Integer.parseInt(orederid),userid);
        request.getRequestDispatcher("/order.do?op=query").forward(request,response);
    }

    public void checkquery(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        OrderService orderService=new OrderServicelmpl();
        String pageNo = request.getParameter("pageNum");
        String ordercode=request.getParameter("ordercode");
        String orderdate=request.getParameter("orderdate");
        String orderflag=request.getParameter("orderflag");
        int pageNum = 1;
        if(pageNo!=null)
            pageNum = Integer.parseInt(pageNo);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("ordercode",ordercode);
        map.put("orderdate",orderdate);
        map.put("orderflag",orderflag);
        CodeService codeService=new CodeServiceImpl();
        List<Code> oderFlags =codeService.queryAll("order");
        map.put("oderFlags",oderFlags);
        Result result =orderService.queryForPage1(map,pageNum);
        int total = orderService.count1(map);
        int pages = (total+ Constant.PAGESIZE-1)/Constant.PAGESIZE;
        PageInfo pageInfo = new PageInfo();
        pageInfo.setList((List)result.getData());
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(Constant.PAGESIZE);
        pageInfo.setTotal(total);
        pageInfo.setPages(pages);
        request.setAttribute("pageInfo",pageInfo);
        request.setAttribute("ordercode",ordercode);
        request.setAttribute("orderdate",orderdate);
        request.setAttribute("orderflag",orderflag);
        request.setAttribute("orderFlags",oderFlags);

        request.getRequestDispatcher("/page/ordersys/order/orderchecklist.jsp").forward(request,response);
    }
    public void checkeup(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String orderid = request.getParameter("orderid");
        String sql = "update biz_order set orderflag=3 where orderid=?";
        Dao.executeSql(sql, orderid);
       Result result=new Result();
       result.setMsg("订单已通过");
       response.getWriter().print(new ObjectMapper().writeValueAsString(result.getMsg()));
    }
    public void checkno(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String orderid = request.getParameter("orderid");
        String sql = "update biz_order set orderflag=4 where orderid=?";
        Dao.executeSql(sql, orderid);
       Result result=new Result();
       result.setMsg("订单未通过");
       response.getWriter().print(new ObjectMapper().writeValueAsString(result.getMsg()));
    }

    public void sbmit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String orederid=request.getParameter("orderid");
        String ordercode = request.getParameter("ordercode");
        String orderflag = request.getParameter("orderflag");
        String[] partsids = request.getParameterValues("partsid");
        String[] counts = request.getParameterValues("ordercount");
        OrderService orderService = new OrderServicelmpl();
        Order order = new Order();
        order.setOrderid(Integer.parseInt(orederid));
        order.setOrdercode(ordercode);
        order.setOrderflag(orderflag);
        order.setOrderdate(new Date());
        List<OrderDetail> details = new ArrayList<OrderDetail>();
        for(int i = 0;i<partsids.length;i++){
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderid(Integer.parseInt(orederid));
            orderDetail.setPartsid(Integer.parseInt(partsids[i]));
            orderDetail.setOrderpartscount(Integer.parseInt(counts[i]));
            details.add(orderDetail);
        }
        User user= (User)request.getSession().getAttribute("user");
        User loginuser=new LoginService().queryID(user.getLoginname());
        String usedid=String.valueOf(loginuser.getUserid());
        Result result = orderService.update(order,details,usedid);
        String sql="update biz_order set orderflag=? where orderid=?";
        Dao.executeSql(sql,orderflag,orederid);
         result.setMsg("提交成功！");
         result.setData(orederid);
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonStr = objectMapper.writeValueAsString(result);
        response.getWriter().print(jsonStr);
    }
}
